const { ccclass, property } = cc._decorator;
import o from "./EngineUtil"

@ccclass
class PageMgr {

    public map_pages = new Map();
    public arr_pageQueue = [];
    public onShowNum = 0;
    public set_onShowPages = new Set();
    public pages = null;
    public persist = null;
    public effects = null;
    public toast = null;
    public message = null;

    constructor() {
        this.map_pages = new Map();
        this.arr_pageQueue = [];
        this.onShowNum = 0;
        this.set_onShowPages = new Set();
        this.pages = null;
        this.persist = null;
        this.effects = null;
        this.toast = null;
        this.message = null;
    }

    static _instance: PageMgr;
    static _getInstance(): PageMgr {
        if (this._instance) return this._instance;
        this._instance = new PageMgr();
        return this._instance;
    };
    public init() {
        this.createNodes();
    };
    public setToastNode(e) {
        e.parent = this.toast;
    };
    public getMessageNode() {
        return this.message;
    };
    public getPagesParent() {
        var e = cc.director.getScene().children[0], t = e.getChildByName("pages");
        if (t) return t;
        (t = new cc.Node("pages")).zIndex = 1;
        e.addChild(t);
        t.width = cc.winSize.width;
        t.height = cc.winSize.height;
        return t;
    };
    public createNodes() {
        var e = new cc.Node("persist");
        this.persist = e;
        var t = new cc.Node("effects");
        t.addComponent("EffectsCtrl");
        this.effects = t;
        var a = new cc.Node("toast");
        this.toast = a;
        var o = new cc.Node("message");
        this.message = o;
        this.persist.addChild(t);
        this.persist.addChild(a);
        this.persist.addChild(o);
        e.setPosition(cc.v2(cc.winSize.width / 2, cc.winSize.height / 2));
        cc.game.addPersistRootNode(e);
    };
    public createEffectsNode() {
        var e = new cc.Node("effects");
        e.addComponent("EffectsCtrl");
        this.effects = e;
        e.setPosition(cc.v2(0, 0));
        cc.director.getScene().children[0].addChild(e, 99);
    };
    public showPage(e, t?) {
        if (e) {
            console.log("showPage name: ", e, " data: ", t);
            this.report2('popup', {
                event_type: e
            })
            var a = this.map_pages.get(e);
            if (a) {
                var o = a.node, i = a.prefab, r = o;
                o && o.isValid || (r = cc.instantiate(i));
                try {
                    this.addPage(e, o, r, i, t);
                } catch (e) {
                    //TODO handle the exception
                    console.error("打开页面错误", JSON.stringify(e));
                    console.error("打开页面错误", e, o, r, i, t);
                }

            } else this.loadPage(e, t);
        } else console.error("class:pageMgr.showPage页面名称为空");
    };
    report2(key, val) {
        this.jsbCall('track', '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V', key, JSON.stringify(val),JSON.stringify({}));
        console.log("上报事件", key, JSON.stringify(val));
    }
    jsbCall(key, type, ...parm) {
        try {
            if (key.indexOf('Close') == -1) {
                console.log('请求andorid ', key, type, ...parm);
            }
            return jsb.reflection.callStaticMethod("com/coolidiom/king/JsAndroidTrans/CocosCallAndroid", key, type, ...parm);
        } catch (error) {
            console.error('调用jsbcall出错' + key);
        }
    }
    public loadPage(e, t) {
        var a = this;
        cc.resources.load("prefabs/" + e, cc.Prefab, function (i, r) {
            i ? console.error("class:pageMgr.fun:showPage加载页面错误", i) : a.addPage(e, null, cc.instantiate(r), r, t);
        });
    };
    public addPage(e, t, a, o, i) {
        var r = null;
        (r = a.getComponent(e + "Ctrl") || a.getComponent("BasePageCtrl")) || (r = a.addComponent(e + "Ctrl"));
        if (!r._only || !this.hasShowPage(e)) {
            var n = 0;
            if (r._inQueue) {
                if (this.onShowNum > 0) {
                    this.arr_pageQueue.push({
                        name: e,
                        data: i
                    });
                    return;
                }
                this.onShowNum++;
            } else n = this.getPageIndex();
            t && t.isValid || (t = a);
            var l = t.getComponent(e + "Ctrl") || t.getComponent("BasePageCtrl");
            r._reuse ? t.active = true : t = a;
            var s = this.getPagesParent();
            this.getPagesParent().getChildByName(e) || s.addChild(t);
            // t.zIndex = n;
            l._init(i);
            this.set_onShowPages.add(t);
            this.map_pages.set(e, {
                node: t,
                prefab: o
            });
        }
    };
    public hidePage(e) {
        if (e) {
            var t = this.map_pages.get(e);
            if (t) {
                var a = t.node;
                if (a) {
                    var o = a.getComponent(e + "Ctrl") || a.getComponent("BasePageCtrl"), i = o._reuse, r = o._inQueue;
                    this.set_onShowPages.delete(a);
                    if (i) o.hide(); else {
                        a.destroy();
                        t.node = null;
                    }
                    if (r) {
                        this.onShowNum--;
                        this.onShowNum < 0 && (this.onShowNum = 0);
                        var n = this.arr_pageQueue.shift();
                        if (n) {
                            var l = n.name, s = n.data;
                            this.showPage(l, s);
                        }
                    }
                } else console.error("class:pageMgr.fun:hidePage没有页面信息node" + e);
            } else console.error("class:pageMgr.fun:hidePage没有页面信息" + e);
        } else console.error("class:pageMgr.fun:hidePage页面名称为空");
    };
    public getPageIndex() {
        var e = 0;
        this.set_onShowPages.forEach(function (t) {
            var a = t.zIndex;
            a >= e && (e = a + 1);
        });
        return e;
    };
    public hasPage(e) {
        return !!this.map_pages.get(e);
    };
    public hasShowPage(e) {
        var t = this.map_pages.get(e);
        if (!t) return false;
        var a = t.node;
        return !!a && !!a.isValid && !!a.active;
    };
    public clear() {
        this.map_pages.forEach((e) => {
            var t = e.prefab, a = e.node;
            cc.assetManager.releaseAsset(t);
            a && cc.isValid(a) && a.destroy();
        });
        this.map_pages.clear();
        this.set_onShowPages.clear();
        this.onShowNum = 0;
        this.arr_pageQueue = [];
        this.pages && this.pages.destroy();
        this.pages = null;
    };
}

export default PageMgr._getInstance();